using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DataPopulation
{
    class Logging
    {
        private StreamWriter swLogFile = null;
        private bool enabled;
        private string path;
        private bool all;

        private Logging()
        {
        }

        public Logging(bool logEnabled, string logPath, bool logAll)
        {
            Enabled = logEnabled;
            LogFilePath = logPath;
            LogAll = logAll;
        }

        public void OpenLogFile()
        {
            if (!Enabled) return;

            string filename = "\\DataPop" + DateTime.Now.ToString("s") + ".log";
            filename = filename.Replace("-", "_");
            filename = filename.Replace(":", "_");
            if (!File.Exists(filename))
            {
                swLogFile = File.CreateText(LogFilePath + filename);
            }
        }

        public void WriteLog(string msg)
        {
            if (swLogFile == null) return;
            swLogFile.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " " + msg);
        }

        public void CloseLogFile()
        {
            if (swLogFile == null) return;
            swLogFile.Flush();
            swLogFile.Close();
            swLogFile = null;
        }

        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        public string LogFilePath
        {
            get { return path; }
            set
            {
                path = value;
                if (value.EndsWith("\\")) path = value.Substring(0, value.Length - 1);
            }
        }

        public bool LogAll
        {
            get { return all; }
            set { all = value; }
        }
    }
}
